package com.ctgu.controller;


import com.ctgu.entity.Account;
import com.ctgu.service.AccountService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author nepture
 */
@Log4j2
@RestController
@RequestMapping("/User")
public class AccountController {

    @Resource
    AccountService service;

    /**
     * todo: 用户从登陆界面输入账号密码后登陆
     *
     * @param account 接收到的账户对象
     * @return 登陆是否成功
     */
    @PostMapping
    public boolean login(@RequestBody Account account) {
        String telephone = account.getTelephone();
        String pwd = account.getPwd();
        log.info("接收到登陆请求,账号为:{},密码为:{}", account.getTelephone(), account.getPwd());
        return service.checkPwdAndAccount(telephone, pwd);
    }

    /**
     * todo: 用户从注册界面注册账号和密码
     *
     * @param account 接收到的账户对象
     * @return 注册
     */
    @PutMapping
    public boolean register(@RequestBody Account account) {
        String telephone = account.getTelephone();
        String pwd = account.getPwd();
        log.info("接收到注册账号的请求,账号为:{},密码为:{}", account.getTelephone(), account.getPwd());
        return service.addAccount(telephone, pwd);
    }

    /**
     * todo: 用户从用户界面注销账号
     *
     * @param telephone 手机号
     * @return 返回是否注销成功
     */
    @DeleteMapping("/{telephone}")
    public boolean delete(@PathVariable("telephone") String telephone) {
        log.info("用户{},注销账号", telephone);
        return service.deleteAccount(telephone);
    }

    @GetMapping
    public String get() {
        return "Hello";
    }
}
